package org.check.UI;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import net.miginfocom.swing.MigLayout;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import java.beans.VetoableChangeListener;



public class DateChooserPanel extends JPanel {

	private DateChooser mpEnd;
	private DateChooser mpStart;
	enum defaultDates{data,course};
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	/**
	 * Create the panel.
	 * @param exportXlsPanel 
	 */
	public DateChooserPanel(defaultDates type, final ExportXlsPanel exportXlsPanel) {
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setBackground(new Color(192, 192, 192));
		setLayout(new MigLayout("", "[12px][155.00px][12px][157.00px]", "[16.00px]"));

		JLabel label = new JLabel("从");
		add(label, "cell 0 0,alignx left,growy");

		JLabel label_1 = new JLabel("到");
		add(label_1, "cell 2 0,alignx left,growy");

		if (type==defaultDates.data) {
			Calendar calendar4chooser = Calendar.getInstance();
			calendar4chooser.set(Calendar.DATE, calendar4chooser.get(Calendar.DATE) - 1);
			mpEnd = new DateChooser(calendar4chooser.getTime(), "yyyy-MM-dd");
			mpEnd.addPropertyChangeListener(new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent evt) {
					exportXlsPanel.setEndDate(getEndTime());
					exportXlsPanel.updateText();
				}
			});
			exportXlsPanel.setEndDate(getEndTime());
			add(mpEnd, "cell 3 0,growx,aligny bottom");
			calendar4chooser.set(Calendar.YEAR, calendar4chooser.get(Calendar.YEAR) - 1);
			mpStart = new DateChooser(calendar4chooser.getTime(), "yyyy-MM-dd");
			mpStart.addPropertyChangeListener(new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent evt) {
					exportXlsPanel.setStartDate(getStartTime());
					exportXlsPanel.updateText();
				}
			});
			exportXlsPanel.setStartDate(getStartTime());
			add(mpStart, "cell 1 0,growx,aligny top");
			
		}else {
			Calendar calendar4chooser = Calendar.getInstance();
			mpEnd = new DateChooser(Calendar.getInstance().getTime(), "yyyy-MM-dd");
			add(mpEnd, "cell 3 0,growx,aligny top");

			calendar4chooser.set(2000, 0, 1);
			mpStart = new DateChooser(calendar4chooser.getTime(), "yyyy-MM-dd");
			add(mpStart, "cell 1 0,growx,aligny top");
		}
	}
	
	public String getStartTime() {
		String result = "";
		Calendar c = mpStart.getSelect();
		result = sdf.format(c.getTime());
		return result;
	}

	public String getEndTime() {
		String result = "";
		Calendar c = mpEnd.getSelect();
		result = sdf.format(c.getTime());
		return result;
	}

}
